// 获取当前激活的标签页

chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
    // 获取标签页的标题和网址
    const tabTitle = tabs[0].title;
    const tabUrl = tabs[0].url;
    console.log(tabTitle);
    console.log(tabUrl)
    // 堆代码 duidaima.com
    // 将标题和网址显示在弹出窗口中
    // title.textContent = tabTitle;
    // url.textContent = tabUrl;
    // 将网址转换为超链接，点击可以打开新标签页
    url.addEventListener("click", function () {
        chrome.tabs.create({ url: tabUrl });
    });
});

// const setBtnDom = document.getElementById("set");
// setBtnDom.onclick = function () {
//     // 向background.js传入消息
//     chrome.runtime.sendMessage({
//       type: "open_set_page",
//     });
// };

chrome.runtime.onMessage.addListener(
    function (request, sender, sendResponse) {
        console.log("popup ",request);
    //   if (request.greeting === "hello") {
    //     sendResponse({ farewell: "goodbye if " })
    //   } else {
    //     sendResponse({ farewell: "goodbye else " })
    //   }
    }
);
chrome.runtime.sendMessage( {greeting:"ready",from:"popup"},function(response) {
    console.log(chrome.fileSystem);
    console.log(response);
});
document.addEventListener('DOMContentLoaded', function()
    {
        console.log('我被执行了！');
        var bg = chrome.extension.getBackgroundPage;
        console.log(bg);
        // bg.onMessage({ data: "hello" });
        const xhr = new XMLHttpRequest();
        xhr.timeout = 3000;
        xhr.open('GET', 'http://172.19.52.67:1987/getMyMusic', true);
        xhr.onreadystatechange = function() {
            // console.log(xhr.readyState);
            if (xhr.readyState === 4 && xhr.status === 200) {
            const responseData = JSON.parse(xhr.responseText);
            var titleOb = document.getElementById("musicTitle");  
            var musicOb = document.getElementById("music");        
            var newList = document.createElement("select");
            newList.setAttribute('id', 'selectId');
            var newListData = document.createTextNode("音乐列表");
            var playOb = document.getElementById("play"); 
            
            newList.size = "20";
            let {code,msg,songs} = responseData;
            for(i = 0;i< songs.length;i++){
                var song = songs[i];
                // newList.appendChild(new Option(song.name,song.path));
                var opt = new Option(song.name, song.path)
                newList.options.add(opt)
            }
            newList.onchange = function (item) {
                // showDiv('other');
                var selectId = document.getElementById("selectId");
                var index = selectId.selectedIndex
                playOb.src = "http://localhost:1987/music"+songs[index].path;
            }
            newList.onselect = function(item) {
                // showDiv('other');
                console.log("onselect"+item);
            }
            newList.onclick = function(item) {
                // showDiv('other');
                console.log(item);
            }
            titleOb.appendChild(newListData);
            musicOb.appendChild(newList);        
            var object = musicOb;
            }
        };
        xhr.ontimeout = function(event){
　　　     alert('请求超时！');
        }
        xhr.send();
    }
);


function play(){
    try{
        // var file = document.getElementById('fileurl').files[0];
        console.log(file);
        var reader = new FileReader();
            reader.onload = (function(file) {
        return function(e){
                    $("audio").attr('src',e.target.result);
                    $("source").attr('src',e.target.result);
                }
            })(file);
        reader.readAsDataURL(file);
    }
    catch(e){   
        alert("同学，该换浏览器啦！！还在忍受龟速浏览器么，用谷歌Chrome吧！"); 
    }  
}

function musicList(){
    const xhr = new XMLHttpRequest();
    xhr.open('GET', 'http://localhost:1987/getMyMusic', true);
    xhr.onreadystatechange = function() {
      if (xhr.readyState === 4 && xhr.status === 200) {
        const responseData = JSON.parse(xhr.responseText);
        console.log(responseData);
      }
    };
    xhr.send();
}